$(function(){
    let id =getQueryString('id')
    if(id){
        $.ajax({
            type:'get',
            url:'http://be.wj0325.top/roles/'+id,
            success:function(res){
            
                $('[name=rolename]').val(res.data.rolename),
                $('[name=email]').val(res.data.email)
            }
        })
    }
})

function btnSave(){
   let id=getQueryString('id');
   let rolename = $('[name=rolename]').val();
   let email = $('[name=email]').val();
   if(id){
    $.ajax({
        type: 'put',
        url: 'http://be.wj0325.top/roles/' + id,
        data: { rolename, email },
        success: function (res) {
        
        }
    })

   }
   else(
    $.ajax({
        type:'post',
        url:'http://be.wj0325.top/roles',
        data:{rolename,email},
        success:function(res){
            
        }
    })
   )
location.href='index.html'
}




//取消并返回
function btnCancel() {
    location.href = 'index.html';
}


function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return decodeURIComponent(r[2]);
    };
    return null;
}